package indi.woniu.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import indi.woniu.entity.FormInline;
import indi.woniu.entity.ResponseEntity;
import indi.woniu.entity.User;
import indi.woniu.entity.UserDo;
import indi.woniu.field.Tables;
import indi.woniu.service.UserService;
import indi.woniu.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtUtils jwtUtils;
    
    //根据id查
    @RequestMapping("/findByRoleId/{id}")
    public ResponseEntity findByRoleId(@PathVariable int id) {
        List<User> userList = userService.findByRoleId(id);
        return new ResponseEntity(200, "成功", userList);
    }
    
    //根据id删除
    @RequestMapping("/deleteById/{id}")
    public ResponseEntity deleteById(@PathVariable int id) {
        int i = userService.deleteById(id);
        return i > 0 ? new ResponseEntity(200, "删除成功", null) : new ResponseEntity(200, "删除失败", null);
    }
    
    @RequestMapping("/updateById/{id}")
    public ResponseEntity updateById(@PathVariable int id) {
        int i = userService.updateById(id);
        return i > 0 ? new ResponseEntity(200, "修改成功", null) : new ResponseEntity(200, "修改失败", null);
        
    }
    
    //查询全部
    @PostMapping("/findAll")
    public ResponseEntity findAll(@RequestBody FormInline formInline) {
        PageHelper.startPage(formInline.getPage(), formInline.getLimit());
        List<User> list = userService.findAll();
        
        PageInfo<User> user = new PageInfo<>(list);
        if (user != null) {
            return new ResponseEntity(200, "查询成功", user);
        }
        return new ResponseEntity(401, "查询失败", user);
    }
    
    /**
     * 获取数据
     */
    @GetMapping("/info")
    @ResponseBody
    public UserDo get(HttpServletRequest req) throws JsonProcessingException {
        String token = req.getHeader("token");
        Claims claims = jwtUtils.parseJwt(token.substring("Bearer ".length()));
        UserDo userDo = userService.selectByName(claims.getSubject());
        userDo.setPassword(null);
        return userDo;
    }
    
    /**
     * 获取所有用户
     */
    @PreAuthorize("hasAnyRole('管理员')")
    @GetMapping("/all")
    @ResponseBody
    public List<UserDo> getUserDo() {
        
        return userService.selectAllUser();
    }
    
    /**
     * 获取所有字段
     */
    @PreAuthorize("hasAnyRole('管理员')")
    @GetMapping("/fields/{table}/{field}")
    @ResponseBody
    public List<String> getFields(@PathVariable("table") Tables table, @PathVariable("field") String field) {
        return userService.selectByGroup(table, field);
    }
    
    /**
     * 更新数据
     * @return 影响行
     */
    @PreAuthorize("hasAnyRole('管理员')")
    @PutMapping("/{id}")
    @ResponseBody
    public Integer update(@PathVariable("id") Integer id,@RequestParam Map<String, Object> field) {
        
        field.put("id", id);
        return userService.update(id, field);
    }
    
}
